package tool.csv;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import tool.RunThis;

	/**
	 * @author jwb09119
	 * @date 2013/11/15
	 * 
	 * This abstract class is extended by any Class that knows how to dump its state into CSV 
	 * format.
	 * 
	 * Re: Overkill - this is an important concept in this application and will likely be reused by
	 * future components that are added.  As well as keeping file writing code in one place where
	 * possible.
	 */

public abstract class CSVCompatible {

	private FileWriter writer;
	
	
	public CSVCompatible (FileWriter writer) {
		
		this.writer = writer;
		
	}
	
	
	public final void exportResults (String exportComment) {
		
		writer.startNewExport ();
		
		writer.writeComment("Java Interface Metric Tool (v" + RunThis.VERSION + ")");
		writer.writeComment("Date: " + getDateAndTimeString());
		writer.writeComment("Series Description: " + exportComment);
		writer.writeComment("File Description: " + getExportHeader());
		writeStateToCSV(writer);
		
		writer.notifyEndOfExport();
		
	}
	
	
	/**
	 * CSV exports must define a string to identify the data being exported
	 * @return - String - usually explaining the nature of the data
	 */
	protected abstract String getExportHeader ();
	
	
	/**
	 * CSV compatible types know hoe to export their own state using the utility methods in 
	 * the {@link FileWriter} class.
	 * @param writer - the fileWriter object to send data to.
	 */
	protected abstract void writeStateToCSV (FileWriter writer);
	
	
	/**
	 * Gets the current date and time in yyyy/MM/dd HH:mm:ss format
	 * @return - String - the date
	 */
	private String getDateAndTimeString () {
		
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date = new Date();
		return dateFormat.format(date);
		
	}
	
}
